package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.dao.exception.DAOException;
import co.gov.medellin.ssti.sub.model.service.AtributosService;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;

public class IngresarRestricAction extends Action {
	@Autowired
	AtributosService atributosServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		HttpSession session = request.getSession();
		// Proceso para la auditoria
		UserVO user = (UserVO) request.getSession().getAttribute("userInfo");
		String terminalAddress = request.getRemoteAddr();
		String strTerminal = ("WEB=" + terminalAddress);
		Calendar c = Calendar.getInstance();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		// Fin del proceso

		String strCodSub = (String) session.getAttribute("codsubpro");
		session.setAttribute("codsubpro", strCodSub);
		String strCodAtri = (String) session.getAttribute("codatributo");
		int continuar = 0;
		Object oboperando1 = request.getParameter("operando1");
		String strOperando1 = "";
		String strOperando = "";
		String strCampo1 = "";
		if (oboperando1 == null) {
			strOperando = request.getParameter("descVflex");
			if (!strOperando.equals("nulo")) {
				String ArrOperando[] = strOperando.split("~");
				String stroper = ArrOperando[0];
				if (!stroper.equals("null")) {
					strOperando1 = ArrOperando[0];
					strCampo1 = ArrOperando[1];
				} else {
					strOperando1 = ArrOperando[1];
					if (strOperando1.equals("nulo")) {
						continuar = 1;
					}
				}
			} else {
				request.setAttribute("msg2",
						"No se selecciono ninguna opcion de la lista de valores");
				request.setAttribute("msg1",
						"La restriccion no se ingreso correctamente");
			}

		} else {
			strOperando1 = request.getParameter("operando1");
		}

		if (continuar == 0) {
			String strOperador = request.getParameter("operador");
			String strOperando2 = request.getParameter("operando2");
			String strDescripcion = request.getParameter("descripcion");
			String strAlerta = request.getParameter("alerta");
			String strVigencia = request.getParameter("vigencia");
			String strArr[] = { strDescripcion, strOperador, strOperando1,
					strOperando2, strAlerta, strVigencia, strCampo1,
					user.getName(), dateFormat.format(c.getTime()),
					strTerminal, user.getName(),
					dateFormat.format(c.getTime()), strTerminal };

			try {
				int iInsertarBien = atributosServiceImpl
						.insertRestriccion(strArr);
				if (iInsertarBien != 0) {
					int iCodMaxRestri = atributosServiceImpl
							.findMaxCodigoRestriccion();
					String strArrAct[] = { user.getName(),
							dateFormat.format(c.getTime()), strTerminal };
					int iActualizarAtri = atributosServiceImpl
							.updateCampoRestAtri(new Long(strCodSub), new Long(
									strCodAtri), new Long(iCodMaxRestri),
									strArrAct);
				} else {
					request.setAttribute("msg1",
							"La restriccion no se ingreso correctamente");
				}
			} catch (DAOException e) {
				SUBException.logDebugError("", e);
			}
		} else {
			request.setAttribute("msg1",
					"La restriccion no se ingreso correctamente");
		}

		return mapping.findForward("success");
	}
}